package com.lovo.controller;

import com.github.pagehelper.PageInfo;
import com.lovo.VO.AuthorityVO;
import com.lovo.model.Authority;
import com.lovo.service.AuthorityService;
import com.lovo.service.impl.AuthorityServiceImpl;
import com.lovo.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "AuthorityServlet",urlPatterns = "/authority.do")
public class AuthorityServlet extends HttpServlet {
    AuthorityService as = new AuthorityServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if (method==null||method.equalsIgnoreCase("")){
            indexByPage(request,response);
        }else if (method==null||method.equalsIgnoreCase("addAuthority")){
            addAuthority(request,response);
        }else if (method==null||method.equalsIgnoreCase("deleteAuthority")){
            deleteAuthority(request,response);
        }else if (method==null||method.equalsIgnoreCase("updateAuthority")){
            updateAuthority(request,response);
        }else if (method==null||method.equalsIgnoreCase("getAllAuthority")){
            getAllAuthority(request,response);
        }
    }

    private void getAllAuthority(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<Authority> list = as.getAllAuthority();
        List<AuthorityVO> authorityVOS = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int key = list.get(i).getAuthorityId();
            String label = list.get(i).getAuthorityName();
            AuthorityVO authorityVO = new AuthorityVO();
            authorityVO.setKey(key);
            authorityVO.setLabel(label);
            authorityVOS.add(authorityVO);
        }
        out.write(MyUtils.toJSON(authorityVOS));
    }

    private void updateAuthority(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int authorityId = Integer.parseInt(request.getParameter("authorityId"));
        String authorityName = request.getParameter("authorityName");
        String authorityType = request.getParameter("authorityType");
        String authorityPath = request.getParameter("authorityPath");
        Authority a = new Authority();
        a.setAuthorityId(authorityId);
        a.setAuthorityName(authorityName);
        a.setAuthorityType(authorityType);
        a.setAuthorityPath(authorityPath);
        boolean flag = as.updateAuthority(a);
        out.write(String.valueOf(flag));
    }

    private void deleteAuthority(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int authorityId = Integer.parseInt(request.getParameter("authorityId"));
        boolean flag = as.deleteAuthority(authorityId);
        out.write(String.valueOf(flag));
    }

    private void addAuthority(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String authorityName = request.getParameter("authorityName");
        String authorityType = request.getParameter("authorityType");
        String authorityPath = request.getParameter("authorityPath");
        Authority a = new Authority();
        a.setAuthorityName(authorityName);
        a.setAuthorityType(authorityType);
        a.setAuthorityPath(authorityPath);
        boolean flag = as.addAuthority(a);
        out.write(String.valueOf(flag));
    }

    private void indexByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int pageNo = 1;
        int pageSize = 10;
        String pageNo1 = request.getParameter("pageNo");
        String pageSize1 = request.getParameter("pageSize");
        if(pageNo1!=null&&!pageNo1.equals("")){
            pageNo = Integer.parseInt(pageNo1);
        }
        if(pageSize1!=null&&!pageSize1.equals("")){
            pageSize = Integer.parseInt(pageSize1);
        }
        String authorityName = request.getParameter("authorityName");
        String authorityPath = request.getParameter("authorityPath");
        Authority a = new Authority();
        a.setAuthorityName(authorityName);
        a.setAuthorityPath(authorityPath);
        PageInfo<Authority>authorityPageInfo = as.listByPage(pageNo,pageSize,a);
        out.write(MyUtils.toJSON(authorityPageInfo));
    }
}
